package com.syqc.monitor.ui.alarm

import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.syqc.adapter.AlarmHistoryAdapter
import com.syqc.comlib.base.BaseToolbarRecycleActivity
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.showLongToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.entity.HistoryAlarm
import com.syqc.monitor.R
import com.syqc.monitor.databinding.HeadNormalInfoBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil

/**
 *@Author Jasper
 *@Time   2020/12/9
 *@Desc   报警列表
 */
class NormalInfoActivity : BaseToolbarRecycleActivity() {
    private var carId = ""
    private var title = ""
    private val mAdapter by lazy { AlarmHistoryAdapter() }

    override fun initView() {
        super.initView()
        title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        ToolbarHelper.setToolbar(this, title)

    }

    override fun initRecyclerView() {
        mRecycler.adapter = mAdapter
        mAdapter.addHeaderView(initHeadView())
        mAdapter.setList(DataUtil.alarmEntity?.list)
    }

    override fun initHeadView(): View {
        val binding = HeadNormalInfoBinding.inflate(layoutInflater)
        val entity = DataUtil.alarmEntity
        binding.lrTvInfoStartTime.setRightText(intent.getStringExtra(Keys.INTENT_START_TIME) ?: "")
        binding.lrTvInfoEndTime.setRightText(intent.getStringExtra(Keys.INTENT_END_TIME) ?: "")

        binding.lrTvInfoLongTime.setRightText("${entity?.long}")
        binding.lrTvInfoTimes.setRightText("${entity?.total} ${getString(R.string.unit_num)}")
        return binding.root
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemClickListener
            val info =
                adapter.data[position] as HistoryAlarm.TotalAlarms
            if (info.total == 0) {
                showLongToast(R.string.toast_no_about_alarm)
            } else {
                startActivityTo<AlarmDetailActivity>(this) {
                    putExtra(Keys.INTENT_CAR_ID, carId)
                    putExtra(Keys.INTENT_PLATE, title)
                    putExtra(Keys.INTENT_TITLE, "${info.name}(${info.total})")
                    DataUtil.alarmInfo = info.list
                }
            }
        }
        binding.tvToTop.setOnClickListener {
            mRecycler.smoothScrollToPosition(0)
        }

        mRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val position = mLayoutManger.findFirstVisibleItemPosition()
                binding.tvToTop.isVisible = position > 0
            }
        })
    }
}